package com.jishuo.nontax.bfc.pzk;

import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * 凭证库发送服务
 */
@Slf4j
public class PzkSocketClient {

    public void  Client(String  content) {
    	String result = null;
		Socket client = null;
    	try {
			NonTaxConfig nonTaxConfig = (NonTaxConfig) SpringContextUtil.getBean("nonTaxConfig");
    		// 1.建立客户端socket连接，指定服务器位置及端口
			client = new Socket();
			SocketAddress socketAddress = new InetSocketAddress(nonTaxConfig.Pzk_IP, nonTaxConfig.Pzk_port);
			client.connect(socketAddress, 3000);
			client.setSoTimeout(nonTaxConfig.API_Socket_TimeOut);
			// 2.得到socket读写流
			OutputStream os = client.getOutputStream();
			PrintWriter pw = new PrintWriter(os);
			// 输入流
			InputStream is = client.getInputStream();
			BufferedReader input = new BufferedReader(new InputStreamReader(is));
			// 3.利用流按照一定的操作，对socket进行读写操作
			String sendInfo = content;
			pw.write(sendInfo);
			pw.flush();
			client.shutdownOutput();

			// 4.关闭资源
			input.close();
			is.close();
			pw.close();
			os.close();
		} catch (Exception e) {
			log.info("凭证库 socket错误：" + e.toString());
			e.printStackTrace();
		} finally {
    		try {
				if (client != null) client.close();
			} catch (Exception e) {}
		}
    }
}